#爬取彼岸壁纸

import re

import requests

for page in range(3,5):
    url = f'http://www.netbian.com/index_{page}.htm'

    headers = {
        'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
    }

    reponse = requests.get(url= url,headers = headers)
    reponse.encoding = reponse.apparent_encoding #解决中文乱码，自动根据所需要的编码格式选择对应的编码

    #解析这个标签：<li><a href="/desk/32677.htm" title="北极熊2023年12月日历桌面壁纸 更新时间：2023-11-27" target="_blank"><img src="http://img.netbian.com/file/2023/1127/small002312AaAyC1701015792.jpg" alt="北极熊2023年12月日历桌面壁纸" />
    image_info = re.findall('<a href="(.*?)" title=".*?" target="_blank"><img src=".*?" alt="(.*?)" />',reponse.text)

    print(image_info)

    for link,title in image_info:
       link_url = "http://www.netbian.com" + link
       reponse_1 = requests.get(url = link_url,headers = headers)
       #解决乱码
       reponse_1.encoding = reponse_1.apparent_encoding
       #注意，带括号标注的:（.*?），代表我们需要的数据，不带（）标注的，代表我们不要的数据
       img_url =  re.findall('<img src="(.*?)" alt=".*?" />',reponse_1.text)[0]
       #获取图片二进制数据
       img_content = requests.get(url = img_url,headers = headers).content
       #写入文件夹
       with open('image\\'+title+'.jpg',mode='wb') as f:
           f.write(img_content)

